package com.spyz.P9_Join_table;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

public class TableMapper extends Mapper<LongWritable, Text, Text, TableBean> {
    private String fileName;
    private Text outK=new Text();
    private TableBean outV=new TableBean();

    @Override
    protected void setup(Mapper<LongWritable, Text, Text, TableBean>.Context context) throws IOException, InterruptedException {
        //初始化 order pd文件
        FileSplit split = (FileSplit) context.getInputSplit();

        fileName = split.getPath().getName();

    }

    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, TableBean>.Context context) throws IOException, InterruptedException {
        // 1、获取一行
        String line = value.toString();
        // 2、判断是哪个文件
        if (fileName.contains("order")){//订单表
            // 3、切割
            String[] split = line.split("\t");
            // 4、封装对象
            outK.set(split[1]);
            outV.setPid(split[1]);
            outV.setId(split[0]);
            outV.setAmount(Integer.parseInt(split[2]));
            outV.setPname("");
            outV.setFlag("order");
        }else {     // 商品表

            String[] split = line.split("\t");

            outK.set(split[0]);
            outV.setPid(split[0]);
            outV.setPname(split[1]);
            outV.setId("");
            outV.setAmount(0);
            outV.setFlag("pd");
        }

        // 写出
        context.write(outK,outV);
    }
}
